<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- header -->
    <%@ include file="/WEB-INF/views/include/header.jsp" %>
</head>
<body class="no-skin">
<%--上传成功模态框--%>
<div class="shade" style="display: none;"></div>
<div class="warning" style="display: none;">
    <p class="title"><em></em><span class="close">X</span></p>
    <p class="cont"></p>
    <div class="la-square-jelly-box la-3x">
        <div></div>
        <div></div>
    </div>
</div>
<div class="modal fade" id="UrgeModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
     data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title" id="myModalLabel">批示</h4> </div>
            <div class="modal-body ">
                <div class="information">
                    <div class="form-group clearfix">
                        <label class="control-label col-sm-2 text-right">姓名</label>
                        <div class="col-sm-9">
                            <input type="text" class="Modelname form-control" readonly >
                        </div>
                    </div>
                    <div class="form-group clearfix">
                        <label class="control-label col-sm-2 text-right">电话</label>
                        <div class="col-sm-9">
                            <input type="text" class="Modelphone form-control" readonly >
                        </div>
                    </div>
                </div>
                <div class="form-group clearfix">
                    <label class="control-label col-sm-2 text-right">常用语句</label>
                    <div class="col-sm-9">
                        <select class="form-control">
                            <option value="">点击选择</option>
                            <option>及时办理</option>
                            <option>速度办理</option>
                        </select>
                    </div>
                </div>
                <div class="form-group clearfix">
                    <label class="control-label col-sm-2 text-right">批示内容</label>
                    <div class="col-sm-9">
                        <textarea rows="3" class="form-control" style="resize: vertical;min-height: 100px"></textarea>
                    </div>
                </div>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success UrgeSure" data-dismiss="modal">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<div class="modal fade" id="approval" tabindex="-1" role="dialog" aria-hidden="true"
     data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">审批</h4> </div>
            <div class="modal-body ">
                <div class="information1">
                    <div class="form-group clearfix">
                        <label class="control-label col-sm-2 text-right">任务名称</label>
                        <div class="col-sm-9">
                            <input type="text" class="mainName form-control" readonly value="">
                        </div>
                    </div>
                    <div class="form-group clearfix">
                        <label class="control-label col-sm-2 text-right">内容</label>
                        <div class="col-sm-9">
                            <textarea type="text" class="Modelcontent form-control" readonly ></textarea>
                        </div>
                    </div>
                    <div class="form-group clearfix">
                        <div class="col-sm-9 col-sm-offset-2">
                            <input type="radio" name="isApproval" value="2">通过
                            <input type="radio" name="isApproval" value="1">不通过
                        </div>
                    </div>
                    <div class="form-group clearfix">
                        <label class="control-label col-sm-2 text-right">审批意见</label>
                        <div class="col-sm-9">
                            <textarea rows="3" class="form-control content"></textarea>
                        </div>
                        <<input type="hidden" class="taskMain" taskMain="${taskMain.sysId}">
                        <input type="hidden" class="sysId" value="">
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success addApproval" data-dismiss="modal">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- /section:basics/navbar.layout -->
<div class="main-container" id="main-container">

    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <!-- /section:settings.box -->
                <div id="taskMainId" style="display: none">
                    ${taskMainId}
                </div>
                <div class="form-group">
                    <h3 class="text-left">
                        任务名称:${taskMain.mainName}
                    </h3>
                </div>
                <c:if test="${! empty taskMain.fileNumber}">
                    <div class="form-group">
                        <div class="col-sm-9">
                            <h3 class="text-left">
                                文号:${taskMain.fileNumber}
                            </h3>
                        </div>
                        <div class="col-sm-3 text-right">
                            <button class="btn-sm btn btn-success allTasks">全部任务</button>
                            <c:if test="${taskMain.status == 0}">
                            <button class="btn-success btn btn-sm approval">审批</button>
                            </c:if>
                        </div>
                    </div>
                </c:if>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <!-- jqGrid表格 -->
                        <table id="gridTable"></table>
                        <!-- jqGrid工具类 -->
                        <div id="gridPager"></div>
                        <!-- 查询对话框 -->
                        <div style="display: none;">
                            <div class="row" id="queryDialog"></div>
                        </div>
                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
                <h4 class="redTips">温馨提示：双击表格行或者点击"查看" 查看任务详情</h4>
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->

    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
    </a>

    <%@ include file="/WEB-INF/views/include/dialog.jsp" %>
</div><!-- /.main-container -->
</div>

<!-- footer -->
<%@ include file="/WEB-INF/views/include/footer.jsp" %>

<script type="text/javascript">
    // 基本路径
    var baseUrl = "<s:url value="/taskMain/copeUnit/"></s:url>"

    // jqgrid的表格高度
    var height = 320;
    // jqgrid表格行头的中文
    var colNames = ['','子事项','责任单位',"单位类型","任务时限", "任务状态","逾期天数","操作"];

    var colModel = [
        {name: 'sysId', width: 60, hidden: true, key: true},
        {name: 'taskSubName', index: 'taskSubName', width: 600, editable: true},
        {name: 'deptName', index: 'deptName', width: 150, editable: true},
        {name: 'unitType', index: 'unitType', width: 100, editable: true,formatter:categoryIdFormatter},
        {name: 'timeLimit', index: 'timeLimit', width: 100, editable: true, },
        {name: 'type', index: 'type', width: 100, editable: true,formatter:typeFormatter},
        {name: 'overdueDay', index: 'overdueDay', width: 150, editable:true },
        {name : 'cz', width : 250, align : "center", formatter:feedbackBtnFormatter}
    ];
    /**
     * 按钮`
     */
    function feedbackBtnFormatter   (value, grid, rows, state) {
                grid.colModel.title=false
                return "<div style='color: #049CE6;display: flex;justify-content: center'>" +
                    "<div style='width: 40px;cursor: pointer' title='查看' id='feedbackBtn'>查看</div>" +
                    // "<div style='border-left:1px solid #049CE6;border-right:1px solid #049CE6;width: 40px;cursor: pointer'title='修改'>修改</div>" +
                    // "<div style='width: 40px;cursor: pointer' title='删除'>删除</div>" +
                    "<div style='width: 40px;cursor: pointer;border-left:1px solid #049CE6;' class='urge' title='批示'>批示</div>" +
                    "</div>";
            }


    /**
     * 逾期类型格式化
     */
    function statusFormatter(data) {
        return data == 0 ? '是 ' : ''
    }
    /**
     * 任务类型格式化
     */
    function categoryIdFormatter(data) {
      switch (data){
          case '1':
              return '牵头';
              break;
          case '2':
              return '协办';
              break;
          default:
              return '未知';
      }
    }

    /**
     * 任务状态格式化
     */
    function typeFormatter(data) {
        switch(data) {
            case '0':
                return '待签收';
                break;
            case '1':
                return '待反馈';
                break;
            case '2':
                return '已反馈';
                break;
            case '3':
                return '退回重办';
                break;
            case '4':
                return '完成';
                break;
            default:
                return '未知';
        }
    }

    /**
     * 附件格式化
     */
    function fileFormatter(data) {
        if (data == null || data == "") {
            return "无任务附件";
        } else {
            return '<input type="button" value="下载" onclick="downDetail(\'' + data + '\')" \n' +
                ' style="width: 100%; height: 32px;" class="btn-large btn-primary"/>';
        }
    }

    //下载按钮
    function downDetail(id) {
        var url = "/filesUpload/download/" + id;
        window.open(url);
    }

    // jqgrid的具体执行方法
    var rowIds;
    var userList;
    jqgrid({
        gridTableId: "#gridTable",
        gridPagerId: "#gridPager",
        postData:{'taskMainId':$('#taskMainId').html().trim()},
        autoScroll: false,
        shrinkToFit: false,
        multiselect:false,
        rownumbers: true,
        onSelectRow: function (rowid, status) {
            rowIds = rowid
            //选择当前的行
            rowids = rowid
            var selectedRowValue = $("#gridTable").jqGrid('getRowData', rowid);
            var types = selectedRowValue.type;//获取任务状态
            //只有已经反馈和待反馈才可以打开
        },
        ondblClickRow:function (rowid) {
            /**
             * 打开二级页面
             */
            window.parent.openapp("/taskMain/unitDetail/"+rowid,rowid,"任务详情");
        },
        loadComplete:function (data) {
            console.log(data);
            var ids = $("#gridTable").jqGrid("getDataIDs");
            console.log(ids);
            var rowDatas = $("#gridTable").jqGrid("getRowData");//获取所有行的数据
            for (var i = 0; i<rowDatas.length;i++){
                if (data.rows[i].type == 2){
                    $("#"+ids[i]+ " td").css("background","yellow"); //已反馈
                }
            }
        },
    });

    //点击开二级页面
    $(document).on("click","#feedbackBtn",function(){
        parent.openapp("/taskMain/unitDetail/"+rowIds,rowIds,"任务详情");
    });

    //    批示
    $(document).on("click",".urge",function () {
        $.get("/taskMain/urgeModel/"+rowIds,function (data) {
            userList = data;
            var html = "";
            for (var i = 0; i < data.length;i++){
                if(data[i].name == undefined) {
                    data[i]['name'] = '' //要报错？
                }
                if(data[i].mobile == undefined) {
                    data[i]['mobile'] = ''  //为空不显示undefined  要报错？
                }
                html += " <div class=\"form-group clearfix\">\n" +
                    "                        <label class=\"control-label col-sm-2 text-right\">姓名</label>\n" +
                    "                        <div class=\"col-sm-9\">\n" +
                    "                            <input type=\"text\" class=\"Modelname form-control\" readonly value='"+data[i].name+"'>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                    <div class=\"form-group clearfix\">\n" +
                    "                        <label class=\"control-label col-sm-2 text-right\">电话</label>\n" +
                    "                        <div class=\"col-sm-9\">\n" +
                    "                            <input type=\"text\" class=\"Modelphone form-control\" readonly value='"+data[i].mobile+"'>\n" +
                    "                        </div>\n" +
                    "                    </div>"
            }

            $(".information").html(html);
        });
        $('#UrgeModel').modal('show'); //显示
    });

    $("#UrgeModel select").change(function () {
        $("#UrgeModel textarea").val( $(this).val());
    })

    $(".UrgeSure").click(function () {
        var datas = {
            taskUnitId: rowIds,
            content: $("#UrgeModel textarea").val(),
            userList:userList
        }
        var jsonDatas = JSON.stringify(datas);
        $.ajax({
            url:'/taskMain/saveHasten/',
            type:'POST',
            contentType: 'application/json; charset=UTF-8',
            async:false,
            dataType:'json',
            data:jsonDatas,
            success: function (data) {
                if (data.code == "200") {
                    alert("批示成功");
                    $("#UrgeModel textarea").val("");
                } else {
                    alert("批示失败");
                }
            }
        })
    })






    //审批
    $(document).on("click",".approval",function () {
        var  sysId = $(".taskMain").attr("taskMain")
        $('#approval').modal('show'); //显示
        $.get("/taskMain/approve/"+sysId,function (data) {
            $(".Modelcontent").val(data.content);
            $(".mainName").val(data.mainName);
            $(".sysId").val(data.sysId);

        })
    });
    $(".addApproval").click(function () {
        var datas = {
            status:$("[name='isApproval']:checked").val(),
            content:$(".information1 .content").val(),
            taskMainId:$(".sysId").val()
        }
        if (!datas.status){
            return alert("请选择是否通过 ")
        }
        $.post("/taskMain/approve/",datas,function (data) {
            if (data.code==200) {
                datas.status == 2 ? alert("通过审批") : alert("不通过审批")
                open(location, '_self').close();
            }else {
                alert("审批失败");
            }
        })
    })
    
    //全部任务 获取到所以任务
    $(".allTasks").click(function () {
        // var newdata;
        var data = $(".taskMain").attr("taskMain");
        $("#gridTable").jqGrid('clearGridData');  //清空表格
        $("#gridTable").jqGrid('setGridParam',{
            url:"/taskMain/allUnit/",
            postData:{"taskMainId":data}, //发送数据
            page:1
        }).trigger("reloadGrid"); //重新载入
    })
</script>

</body>
</html>